package mj.scott.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mj.scott.common.Result;
import mj.scott.pojo.entity.TableInfo;
import mj.scott.service.ITableInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/table")
@Api(tags = "桌号管理")
public class TableInfoController {

    @Autowired
    private ITableInfoService tableInfoService;

    /**
     * 新增桌号
     */
    @PostMapping
    @ApiOperation("新增桌号（自动生成二维码）")
    public Result<TableInfo> addTable() {
        TableInfo tableInfo = tableInfoService.addTable();
        return Result.success(tableInfo);
    }

    /**
     * 删除桌号
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除桌号（含二维码）")
    public Result<String> deleteTable(@PathVariable Long id) {
        tableInfoService.deleteTable(id);
        return Result.success("桌号删除成功");
    }

    /**
     * 查询所有桌号（供前端展示列表）
     */
    @GetMapping
    @ApiOperation("查询所有桌号")
    public Result<List<TableInfo>> getAllTables() {
        List<TableInfo> tableList = tableInfoService.list();
        return Result.success(tableList);
    }


    //用户部分



    @GetMapping("/verify")
    @ApiOperation("验证桌号有效性")
    public Result<TableInfo> verifyTable(@RequestParam String tableNumber) {
        LambdaQueryWrapper<TableInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TableInfo::getTableNumber, tableNumber)
                .eq(TableInfo::getStatus, 1); // 只验证启用状态的桌号
        TableInfo tableInfo = tableInfoService.getOne(queryWrapper);
        if (tableInfo == null) {
            return Result.error("桌号无效");
        }
        return Result.success(tableInfo);
    }
}